{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "%matplotlib inline\n",
    "from pycocotools.coco import COCO\n",
    "import numpy as np\n",
    "import skimage.io as io\n",
    "import matplotlib.pyplot as plt\n",
    "import pylab"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "image_directory = 'train/shapes_train2018/'\n",
    "annotation_file = 'train/annotations/instances_shape_train2018.json'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "loading annotations into memory...\n",
      "Done (t=0.00s)\n",
      "creating index...\n",
      "index created!\n"
     ]
    }
   ],
   "source": [
    "example_coco = COCO(annotation_file)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Custom COCO categories: \n",
      "square circle triangle\n",
      "\n",
      "Custom COCO supercategories: \n",
      "shape\n"
     ]
    }
   ],
   "source": [
    "categories = example_coco.loadCats(example_coco.getCatIds())\n",
    "category_names = [category['name'] for category in categories]\n",
    "print('Custom COCO categories: \\n{}\\n'.format(' '.join(category_names)))\n",
    "\n",
    "category_names = set([category['supercategory'] for category in categories])\n",
    "print('Custom COCO supercategories: \\n{}'.format(' '.join(category_names)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "category_ids = example_coco.getCatIds(catNms=['square'])\n",
    "image_ids = example_coco.getImgIds(catIds=category_ids)\n",
    "image_data = example_coco.loadImgs(image_ids[np.random.randint(0, len(image_ids))])[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'coco_url': '',\n",
       " 'date_captured': '2018-04-13 02:36:13.581004',\n",
       " 'file_name': '1000.jpeg',\n",
       " 'flickr_url': '',\n",
       " 'height': 128,\n",
       " 'id': 1,\n",
       " 'license': 1,\n",
       " 'width': 128}"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "image_data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAQUAAAD8CAYAAAB+fLH0AAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDIuMi4wLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvFvnyVgAAEOdJREFUeJzt3c2O61paxvHnXXbV3qcP3TCAlhoxQQwYg4TEiCvoG+BGuCMEN8GIMY2EesIIJAZILYFoDt1nV5W9Xgbrw34TZ+/K2ZXESv1/kyS281Fl5/H6dMzdBQBNuvUHALAvhAKAgFAAEBAKAAJCAUBAKAAICAUAAaEAICAUAATjrT+AJP3tL37OsMo3YPlRsuf2QJLkNfddo0yTJGnwuWwiadYHSdKcHuttls0vkqRHL4dHziZJmixpHsuu+pCfLvzXvA+WRyXl+qjc5rrvcsoylX01eFk2Zsm97A8NHyVJU0ryuk81lf3/kOr+N9M8l3V//Rf/YK/5TJQUAAS7KCngjdjUSwjSVuGrnQPm1bJ6RvGpPnxRqiWKZPU2jf3p2cpzvb8Pvool5cOSQt1NpYBWHljdnS5JNkiSnuay/ctsGsaybBxLie+llQbNpIfHsz4SoXBXsrbDQCoHV/wie3+OpFSKnWazkpf7PpctBn+oL+89KHJ6eLNP/Z4lPR/tslpbU6klWF1WvvSzrULaa3qkpDmFJ6nuJs3uy/av/kwAsEJJ4e4d5347EyVfqgHWlqWsVGsXo5ezk+byGuNgSrXxcZ5f1WaFL8rLGb+y+thX1QfVhkbP0lSrDdNQ990wavZYBUl115mknM5rx6ekACCgpHB3Tp/B/XALy/JWZ60n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RwR5IfnzHWF1bpZ43PnD7WJYs2kKlXU8M6Dp034ev/48G091Ub0dY++NzApLnv4vPP+5QUAATE/Z1JfjjMta/R4ZklbwyPLVvGH2NZBr9sjcXH10g+rOaRHPxS1MbFUdyH3o0Z9t9h21HYP/zuw7s1p8+PMjxsiBxe3X3NPOlLmVc/DbfMczi9/XpV2H9H+/J11YwtxD2AgFAAEBAKAAJCAUBAKAAICAUAAaEAICAUAASEAoCAUAAQEAoAAkIBQEAoAAgIBQABoQAgIBQABIQCgIBQABAQCgACQgFAQCgACAgFAAGhACAgFAAEhAKAgFAAEBAKAAJCAUBAKAAICAUAAaEAICAUAASEAoCAUAAQEAoAAkIBQEAoAAgIBQABoQAgIBQABIQCgIBQABAQCgACQgFAQCgACAgFAAGhACAgFAAEhAKAgFAAEBAKAAJCAUBAKAAICAUAAaEAICAUAASEAoCAUAAQEAoAAkIBQEAoAAgIBQABoQAgIBQABIQCgIBQABAQCgACQgFAQCgACAgFAAGhACAgFAAEhAKAgFAAEBAKAAJCAUBAKAAICAUAAaEAICAUAASEAoDA3P3WnwHAjlBSABAQCgACQgFAQCgACAgFAAGhACAgFAAEhAKAgFAAEBAKAAJCAUBAKAAICAUAAaEAICAUAASEAoCAUAAQEAoAAkIBQEAoAAgIBQABoQAgIBQABP8Pmz8/1YwK474AAAAASUVORK5CYII=\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# load and display instance annotations\n",
    "image = io.imread(image_directory + image_data['file_name'])\n",
    "plt.imshow(image); plt.axis('off')\n",
    "pylab.rcParams['figure.figsize'] = (8.0, 10.0)\n",
    "annotation_ids = example_coco.getAnnIds(imgIds=image_data['id'], catIds=category_ids, iscrowd=None)\n",
    "annotations = example_coco.loadAnns(annotation_ids)\n",
    "example_coco.showAnns(annotations)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
